package zsd.reiview.utility;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Pattern;

/**
 * @Title: PmiUtility.java
 * @Package zsd.reiview.utility
 * @Description: TODO(添加描述)
 * @author 朱圣代
 * @date 2011-10-11 下午03:48:35
 * @version V1.0
 */
public class PmiUtility {

	/**
	 * @Title: main
	 * @Description:
	 * @param @param args
	 * @return void
	 * @throws IOException
	 * @throws
	 * @date 2011-10-11 下午03:48:35
	 */

	public static String searchNumStr(String preurl, String word, String encode, String reg) throws IOException {
		String url = preurl + URLEncoder.encode(word, encode);
		FileDownByHttpClient fileDown = new FileDownByHttpClient();
		String data = fileDown.downloadFile(url, encode);
		FileUtility.writeString("G://1.txt", data);
		String regex = new String(reg.getBytes(encode), "utf-8");
		// Pattern pattern = Pattern.compile(regex);
		String match = StringUtility.getMatch(data, regex, 1);
		return match;
	}

	public static double yahooPmi(String a, String b) throws Exception {
		String[] mats = new String[] { searchNumStr("http://www.yahoo.cn/s?q=", a, "utf-8", "找到相关网页约([0-9,]+)条"), searchNumStr("http://www.yahoo.cn/s?q=", b, "utf-8", "找到相关网页约([0-9,]+)条"),
				searchNumStr("http://www.yahoo.cn/s?q=", a + " " + b, "utf-8", "找到相关网页约([0-9,]+)条") };
		long[] nums = new long[3];
		for (int i = 0; i < mats.length; i++) {
			nums[i] = Long.parseLong(mats[i].replaceAll(",", ""));
		}

		return (double) Math.log((double) nums[2] / nums[0] * nums[1]);
	}

	public static void main(String[] args) throws Exception {

		System.out.println(PmiUtility.yahooPmi("手机", "屏幕"));
	}

	public static class FileDownByHttpClient {

		public String downloadFile(String url, String encode) {
			return null;
		}

	}

}
